package com.iwhalecloud.citybrain.flow.platform.manager.common;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author lilh
 * @date 2019-01-20 13:58
 */
public class ResponseResult<T> {



    private boolean success;

    private int errCode;
    private String errMsg;
    private String errName;
    private int totalCount;
    private T datas;
    private int pageNum;
    private int pageSize;

    private ResponseResult() {
        this.success = true;
        this.errCode = 0;
        this.errMsg = "success";
        this.errName = "";
        this.totalCount = 0;
        this.datas = null;
        this.pageNum = 1;
        this.pageSize = 10;
    }

    public static <T> ResponseResult<T> success() {
        return success(null);
    }

    public static <T> ResponseResult<T> success(T datas) {
        return new ResponseResult<T>()
                .setSuccess(true)
                .setErrCode(0)
                .setErrMsg("success")
                .setErrName(null)
                .setDatas(datas);
    }

    public static <T> ResponseResult<T> fail(int errCode) {
        return fail(errCode, null, null, null);
    }

    public static <T> ResponseResult<T> fail(int errCode, String errMsg) {
        return fail(errCode, errMsg, null, null);
    }

    public static <T> ResponseResult<T> fail(int errCode, String errMsg, String errName, T datas) {
        return new ResponseResult<T>()
                .setSuccess(false)
                .setErrCode(errCode)
                .setErrMsg(errMsg)
                .setErrName(errName)
                .setDatas(datas);
    }

    public boolean isSuccess() {
        return this.success;
    }

    public int getErrCode() {
        return this.errCode;
    }

    public String getErrMsg() {
        return this.errMsg;
    }

    public String getErrName() {
        return this.errName;
    }

    public int getTotalCount() {
        return this.totalCount;
    }

    public Object getDatas() {
        return this.datas;
    }

    public int getPageNum() {
        return this.pageNum;
    }

    public int getPageSize() {
        return this.pageSize;
    }


    public ResponseResult<T> setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public ResponseResult<T> setErrCode(int errCode) {
        this.errCode = errCode;
        return this;
    }

    public ResponseResult<T> setErrMsg(String errMsg) {
        this.errMsg = errMsg;
        return this;
    }

    public ResponseResult<T> setErrName(String errName) {
        this.errName = errName;
        return this;
    }

    public ResponseResult<T> setTotalCount(int totalCount) {
        this.totalCount = totalCount;
        return this;
    }

    public ResponseResult<T> setDatas(T datas) {
        this.datas = datas;
        return this;
    }

    public ResponseResult<T> setPageNum(int pageNum) {
        this.pageNum = pageNum;
        return this;
    }

    public ResponseResult<T> setPageSize(int pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    @Override
    public String toString() {
        try {
            return new ObjectMapper().writeValueAsString(this);
        } catch (JsonProcessingException e) {
            String error = "convert object to json failed";
            throw new IllegalArgumentException(error);
        }
    }

    /**
     * 转换resultForService对于前端格式返回
     * @param resultForService
     * @return
     */
    public static ResponseResult response(ResultForService resultForService) {
        if (ResultForService.SUCCESS.equals(resultForService.getResultCode())) {
            return ResponseResult.success(resultForService.getData());
        } else {
            return ResponseResult.fail(Integer.valueOf(resultForService.getResultCode()),
                    resultForService.getResultMsg());
        }
    }

    /**
     * 转换resultForServicePage对于前端格式返回
     * @param resultForService
     * @return
     */
    public static ResponseResult response(ResultPageForService resultForService) {
        if (ResultForService.SUCCESS.equals(resultForService.getResultCode())) {
            return new ResponseResult().setSuccess(true).setDatas(resultForService.getData())
                    .setTotalCount(resultForService.getTotalCount());
        } else {
            return ResponseResult.fail(Integer.valueOf(resultForService.getResultCode()),
                    resultForService.getResultMsg());
        }
    }

}
